<?php

namespace plugin\admin\app\controller;

use plugin\admin\app\common\Util;
use plugin\admin\app\common\fast\Pinyin;
use plugin\admin\app\constant\v1\CommonConstant;
use support\Request;
use support\Response;
use plugin\admin\app\model\V1Game;
use plugin\admin\app\controller\Crud;
use support\Db;
use support\exception\BusinessException;

/**
 * 游戏管理
 */
class V1GameController extends Crud
{
    /**
     * 无需登录及鉴权的方法
     * @var array
     */
    protected $noNeedLogin = ['select2'];

    /**
     * 需要登录无需权的方法
     * @var array
     */
    protected $noNeedAuth = [];

    /**
     * @var V1Game
     */
    protected $model = null;

    /**
     * 构造函数
     * @return void
     */
    public function __construct()
    {
        $this->model = new V1Game;
    }

    /**
     * 浏览
     * @return Response
     */
    public function index(): Response
    {
        return view('v1-game/index');
    }

    /**
     * 查询
     * @param Request $request
     * @return Response
     * @throws BusinessException
     */
    public function select(Request $request): Response
    {
        [$where, $format, $limit, $field, $order] = $this->selectInput($request);
        $where['deleted'] = CommonConstant::DELETE_0;
        $query = $this->doSelect($where, $field, $order);
        return $this->doFormat($query, $format, $limit);
    }

    /**
     * 查询
     * @param Request $request
     * @return Response
     * @throws BusinessException
     */
    public function select2(Request $request): Response
    {
        [$where, $format, $limit, $field, $order] = $this->selectInput2($request);
        $where['deleted'] = CommonConstant::DELETE_0;
        $query = $this->doSelect($where, $field, $order);
        $query->select(['id','name']);
        return $this->doFormat($query, $format, $limit);
    }

    /**
     * 插入
     * @param Request $request
     * @return Response
     * @throws BusinessException
     */
    public function insert(Request $request): Response
    {
        if ($request->method() === 'POST') {

            $params = $request->post();

            $name_array = getLineBreak($params['name']);
            if($params['check'] == CommonConstant::DEFAULT_2){
                $process_name_array = getLineBreak($params['process_name']);
            }

            $insert_data = [];
            $date = date('Y-m-d H:i:s');
            for ($i = 0; $i < count($name_array); $i++) {
                $name = $name_array[$i];
                $pinyin = Pinyin::get($name,true,'-',true);
                $letter = explode(' ',$pinyin)[0];

                $data = [
                    'type' => $params['type'],
                    'letter' => $letter,
                    'name' => $name,
                    'created_at' => $date,
                ];
                $data['process_name'] = $params['process_name'];
                if($params['check'] == CommonConstant::DEFAULT_2){
                    $data['process_name'] = $process_name_array[$i];
                }
                $insert_data[] = $data;
            }

            Util::db()->transaction(function () use ($insert_data) {
                $this->model->insert($insert_data);
            });
            return $this->json(0, 'ok', []);
            // return parent::insert($request);
        }
        $type = $request->get('type');
        return view('v1-game/insert',['type'=>$type]);
    }

    /**
     * 更新
     * @param Request $request
     * @return Response
     * @throws BusinessException
     */
    public function update(Request $request): Response
    {
        if ($request->method() === 'POST') {
            [$id, $data] = $this->updateInput($request);

            $pinyin = Pinyin::get($data['name'],true,'-',true);
            $letter = explode(' ',$pinyin)[0];
            $data['letter'] = $letter;

            $this->doUpdate($id, $data);
            return $this->json(0);
//            return parent::update($request);
        }
        return view('v1-game/update');
    }

    /**
     * 删除
     * @param Request $request
     * @return Response
     * @throws BusinessException
     */
    public function delete(Request $request): Response
    {
        $ids = $this->deleteInput($request);
        $this->doDeleted($ids);
        return $this->json(0);
    }

}
